package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.constants.TableNameConstants;
import com.credithc.customers.dataservice.ro.BinlogRo;
import com.credithc.customers.dataservice.ro.CustomerAccountInfoRo;
import com.credithc.customers.dataservice.ro.CustomerProductInfoRo;
import com.credithc.customers.dataservice.ro.ProductInfoRo;
import com.credithc.customers.dataservice.service.*;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DataConversionServiceImpl implements DataConversionService {
    @Autowired
    ProductInfoService productInfoService;
    @Autowired
    CustomerProductInfoService customerProductInfoService;
    @Autowired
    SyncDataService syncDataService;
    @Autowired
    CustomerAccountInfoService customerAccountInfoService;
    Gson gson = new Gson();
    @Override
    public void binlogConversion(String binlog) {
        List<ProductInfoRo> productInfoRos = new ArrayList<>();
        List<CustomerAccountInfoRo> customerAccountInfoRos = new ArrayList<>();
        List<CustomerProductInfoRo> customerProductInfoRos = new ArrayList<>();
        Gson gson = new Gson();
        String[] json = binlog.split("\n");
        for (int i= 0;i<json.length;i++){
            BinlogRo binlogRo = gson.fromJson(json[i], BinlogRo.class);
            String tableName = binlogRo.getTableName();
            if (TableNameConstants.DLR_CUSTOMER_PRODUCT_QM_LEND_ORDER.equals(tableName)){
                CustomerProductInfoRo customerProductInfoRo = customerProductInfoService.bulidDlrCustomerProduct(binlogRo);
                if(customerProductInfoRo!=null && StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo())) {
                    customerProductInfoRos.add(customerProductInfoRo);
                }
               // log.info("------------------预备向订单表添加一条记录----------------------");
            }
            if (TableNameConstants.DLR_ACCOUNT_QM_USER.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidDlrAccount(binlogRo);
                if (customerAccountInfoRo!=null && StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo()) && StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo())) {
                    customerAccountInfoRos.add(customerAccountInfoRo);
                   // log.info("------------------预备向账户表添加一条记录----------------------");
                }
            }
            if (TableNameConstants.DLR_QM_USER_CARD.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidDlrBankAccount(binlogRo);
                if (customerAccountInfoRo !=null && StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())  && StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo()) ) {
                    customerAccountInfoRos.add(customerAccountInfoRo);
                  //  log.info("------------------预备向账户表添加一条记录----------------------");
                }else if (StringUtils.isBlank(customerAccountInfoRo.getAccountNo()) && SyncTypeEnum.DELETE.getValue().equals(customerAccountInfoRo.getSyncType())){
                    customerAccountInfoRos.add(customerAccountInfoRo);
                  //  log.info("------------------预备向账户表添加一条记录----------------------");
                }
            }

            if (TableNameConstants.DLR_PRODUCT_QM_PRODUCT_MANAGER.equals(tableName)){
                ProductInfoRo productInfoRo = productInfoService.bulidDlrProduct(binlogRo);
                productInfoRos.add(productInfoRo);
               // log.info("------------------预备向产品表添加一条记录----------------------");
            }
            //hyd
            if (TableNameConstants.HYD_CUSTOMER_PRODUCT_JSD_LOAN_ORDER.equals(tableName)){
                CustomerProductInfoRo customerProductInfoRo = customerProductInfoService.bulidHydCustomerProduct(binlogRo);
                if (customerProductInfoRo != null && StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo())) {
                    customerProductInfoRos.add(customerProductInfoRo);
                   // log.info("------------------预备向订单表添加一条记录----------------------");
                }
            }
            if (TableNameConstants.HYD_BANK_TABLE.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidHydBankAcount(binlogRo);
                if (customerAccountInfoRo!= null && StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo())&&StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())){
                    customerAccountInfoRos.add(customerAccountInfoRo);
                }
            }
            //HYR产品表
            if (TableNameConstants.HYR_PRODUCT_DTPAY.equals(tableName)){
                ProductInfoRo productInfoRo = productInfoService.bulidHyrProduct(binlogRo);
                productInfoRos.add(productInfoRo);
               // log.info("------------------预备向产品表添加一条记录----------------------");
            }
            //hyr正常出借预约出借表
            if ("lenduinfo".equals(tableName)){
                CustomerProductInfoRo customerProductInfoRo = customerProductInfoService.bulidHyrFixCustomerProduct(binlogRo);
                if (customerProductInfoRo!= null && StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo()) && StringUtils.isNotBlank(customerProductInfoRo.getUserResourceId())) {
                    customerProductInfoRos.add(customerProductInfoRo);
                }
              //  log.info("------------------预备向订单表添加一条记录----------------------");
            }
          /*  if (TableNameConstants.HYR_CUSTOMER_PRODUCT_HYR_THROW_NEW.equals(tableName)){
                CustomerProductInfoRo customerProductInfoRo = customerProductInfoService.bulidHyrThrowCustomerProduct(binlogRo);
                customerProductInfoRos.add(customerProductInfoRo);
                log.info("------------------预备向订单表添加一条记录----------------------");
            }
            if (TableNameConstants.HYR_CUSTOMER_PRODUCT_ORDERCRIGHT.equals(tableName)){
                CustomerProductInfoRo customerProductInfoRo = customerProductInfoService.bulidHyrCrightCustomerProduct(binlogRo);
                customerProductInfoRos.add(customerProductInfoRo);
                log.info("------------------预备向订单表添加一条记录----------------------");
            }*/
            //hyr账户表
            if (TableNameConstants.HYR_ACCOUNT_HYR_USER_MESSAGE.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidHyrAccount(binlogRo);
                if (customerAccountInfoRo != null &&  StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo()) && StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo()) ) {
                    customerAccountInfoRos.add(customerAccountInfoRo);
                //    log.info("------------------预备向账户表添加一条记录----------------------");
                }
            }
            //hyr银行卡表
            if (TableNameConstants.HYR_ACCOUNT_BANCK_CARD.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidHyrBankAccount(binlogRo);
                if (customerAccountInfoRo != null && StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo()) && StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo()) ) {
                    customerAccountInfoRos.add(customerAccountInfoRo);
                 //   log.info("------------------预备向账户表添加一条记录----------------------");
                }
            }
            //xhqb
            if (TableNameConstants.XHQB_CUSTOMER_PRODUCT_HW_ORDER.equals(tableName)){
                CustomerProductInfoRo customerProductInfoRo = customerProductInfoService.bulidXhqbCustomerProduct(binlogRo);
                if (customerProductInfoRo!=null && StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo())) {
                    customerProductInfoRos.add(customerProductInfoRo);
                  //  log.info("------------------预备向订单表添加一条记录----------------------");
                }
            }
            if (TableNameConstants.XHQB_BANK_CARD.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidXhqbBankAccount(binlogRo);
                if (StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())) {
                    customerAccountInfoRos.add(customerAccountInfoRo);
                 //   log.info("------------------预备向账户表添加一条记录----------------------");
                }
            }
            //jhzf
            if (TableNameConstants.JHZF_BANK.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidJhzfBankAccount(binlogRo);
                if (StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())) {
                    customerAccountInfoRos.add(customerAccountInfoRo);
                    log.info("------------------预备向账户表添加一条记录----------------------");
                }
            }
            if (TableNameConstants.JHZF_USER.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidJhzfAccount(binlogRo);
                if (customerAccountInfoRo!= null && StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())) {
                    customerAccountInfoRos.add(customerAccountInfoRo);
                  // log.info("------------------预备向账户表添加一条记录----------------------");
                }
            }
            //个贷
            if (TableNameConstants.GD_JJ.equals(tableName)){
                CustomerProductInfoRo customerProductInfoRo = customerProductInfoService.bulidGdCustomerProduct(binlogRo);
                if (customerProductInfoRo!= null && StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo()) && !"0".equals(customerProductInfoRo.getCustomerNo())) {
                    customerProductInfoRo.setUserResourceId("PHGD"+customerProductInfoRo.getCustomerNo());
                    customerProductInfoRos.add(customerProductInfoRo);
                 //   log.info("------------------预备向订单表添加一条记录----------------------");
                }
            }
            if ("crm_intopieces_info_dk".equals(tableName)){
                CustomerProductInfoRo customerProductInfoRo = customerProductInfoService.newGdCustomerProduct(binlogRo);
                if (customerProductInfoRo!= null && StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo())&&!"0".equals(customerProductInfoRo.getCustomerNo())) {
                    customerProductInfoRo.setUserResourceId("PHGD"+customerProductInfoRo.getCustomerNo());
                    customerProductInfoRos.add(customerProductInfoRo);
                 //   log.info("------------------预备向订单表添加一条记录----------------------");
                }
            }
            if ("crm_account_mes_dk".equals(tableName)){

                CustomerProductInfoRo customerProductInfoRo = customerProductInfoService.updataGdCustomerProduct(binlogRo);
                if (customerProductInfoRo!= null && StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo()) &&!"0".equals(customerProductInfoRo.getCustomerNo())) {
                    customerProductInfoRo.setUserResourceId("PHGD"+customerProductInfoRo.getCustomerNo());
                    customerProductInfoRos.add(customerProductInfoRo);
                  //  log.info("------------------预备向订单表添加一条记录----------------------");
                }

            }
            if ("crm_repay_dk".equals(tableName)){
                CustomerProductInfoRo customerProductInfoRo = customerProductInfoService.repayGdCustomerProduct(binlogRo);
                if (customerProductInfoRo!= null && StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo()) &&!"0".equals(customerProductInfoRo.getCustomerNo())) {
                    customerProductInfoRo.setUserResourceId("PHGD"+customerProductInfoRo.getCustomerNo());
                    customerProductInfoRos.add(customerProductInfoRo);
                  //  log.info("------------------预备向订单表添加一条记录----------------------");
                }
            }

            //小贷
            if (TableNameConstants.XD_BANK.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidXdAccount(binlogRo);
               /* if (StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())){
                    customerAccountInfoRos.add(customerAccountInfoRo);
                   // log.info("------------------预备向账户表添加一条记录----------------------");
                }*/
            }
            //信托
            if (TableNameConstants.XT_BANK.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidXtAccount(binlogRo);
               /* if (StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())){
                    customerAccountInfoRos.add(customerAccountInfoRo);
                 //   log.info("------------------预备向账户表添加一条记录----------------------");
                }*/
            }
            //恒慧融借款端 银行卡
            if (TableNameConstants.HHR_JK_BANK.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidHhrJkBankAccount(binlogRo);
                if (StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo()) && StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo()) && !"0".equals(customerAccountInfoRo.getAccountNo())){
                    customerAccountInfoRos.add(customerAccountInfoRo);
                  //  log.info("------------------预备向账户表添加一条记录----------------------");
                }
            }
            //恒慧融借款端财务账户
            if (TableNameConstants.HHR_JK_ACCOUNT.equals(tableName)){
                CustomerAccountInfoRo customerAccountInfoRo = customerAccountInfoService.bulidHhrJkAccount(binlogRo);
                if (StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo()) && StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo()) && !"0".equals(customerAccountInfoRo.getAccountNo())){
                    customerAccountInfoRos.add(customerAccountInfoRo);
                   // log.info("------------------预备向账户表添加一条记录----------------------");
                }
            }
            //保险保贷帮
            //产品表
            if ("hrhb_productdef".equals(tableName)){
                ProductInfoRo productInfoRo = productInfoService.bulidBDBProduct(binlogRo);
                productInfoRos.add(productInfoRo);
              //  log.info("------------------预备向产品表添加一条记录----------------------");
            }
            //订单表
            if ("hrhb_order_insure".equals(tableName)){
                customerProductInfoService.buildBDBCustomerProduct(binlogRo);
            }
            //基金 产品
            if ("ppf_fund".equals(tableName)){
                ProductInfoRo productInfoRo = productInfoService.bulidJJProduct(binlogRo);
               // productInfoRos.add(productInfoRo);
              //  log.info("------------------预备向产品表添加一条记录----------------------");
            }
            //基金 订单 账户
            if ("ppf_customer".equals(tableName)){
                customerProductInfoService.bulidFundData(binlogRo);
            }
            //保险 核心保险
            if ("sale_policy".equals(tableName)){
                customerProductInfoService.bulidBXHXData(binlogRo);
            }
            //产品表
            if ("pro_info".equals(tableName)){
                ProductInfoRo productInfoRo = productInfoService.bulidHxbxProduct(binlogRo);
                productInfoRos.add(productInfoRo);
            }
            //车贷
            //产品表
            if ("FL_Prod_Base".equals(tableName)){
                ProductInfoRo productInfoRo = productInfoService.bulidChdProduct(binlogRo);
                productInfoRos.add(productInfoRo);
            }
            //订单表
            if ("fl_business_common".equals(tableName)){
                customerProductInfoService.bulidChdData(binlogRo);
            }
            //账户表
            if ("fl_contract".equals(tableName)){
                customerAccountInfoService.bulidChdAccount(binlogRo);
            }
            //境外保险
            //订单
            if ("oi_proposalform".equals(tableName)){
                customerProductInfoService.bulidJwbxOrder(binlogRo);
            }
            // 产品
            if ("oi_products".equals(tableName)){
                ProductInfoRo productInfoRo = productInfoService.bulidJwbxProduct(binlogRo);
                productInfoRos.add(productInfoRo);
            }

            //恒慧融出借端
            //产品
            if ("f_product".equals(tableName)){
                ProductInfoRo productInfoRo = productInfoService.bulidhhrcjdProduct(binlogRo);
                productInfoRos.add(productInfoRo);
            }
            //账户
            if ("f_user_info".equals(tableName)){
                customerAccountInfoService.bulidHHRCJAccount(binlogRo);
            }
            //订单
            if ("f_lender_detail".equals(tableName)){
                customerProductInfoService.bulidHHRCJOrder(binlogRo);
            }
        }
        try {
            if (productInfoRos.size()>0) { syncDataService.syncProduct(productInfoRos); }
        } catch (Exception e) {
            log.error("************产品表插入失败，失败数据为："+gson.toJson(productInfoRos));
        }
        try {
            if (customerAccountInfoRos.size()>0){syncDataService.syncCustomerAccount(customerAccountInfoRos); }
        } catch (Exception e) {
            log.error("************账户表插入失败，失败数据为："+gson.toJson(customerAccountInfoRos));
        }
        try {
            if (customerProductInfoRos.size()>0){syncDataService.syncCustomerProduct(customerProductInfoRos);}
        } catch (Exception e) {
            log.error("************订单表插入失败，失败数据为："+gson.toJson(customerProductInfoRos));
        }
    }
}
